<?php

require_once dirname(__FILE__) . '/../lib/tradeGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/tradeGeneratorHelper.class.php';

/**
 * trade actions.
 *
 * @package    truetrader
 * @subpackage trade
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class tradeActions extends autoTradeActions
{

  public function executeShow(sfWebRequest $request)
  {
    $this->trade = $this->getRoute()->getObject();
    $msg = 'free';
    if ($this->trade->getTrader()->getFollowerAccessId() != FollowerAccessTable::FREE)
    {
      $msg = 'promotional';
      if (!$this->trade->isPromotional())
      {
        $msg = 'follower';
        if (!$this->getUser()->getTrader()->isFollowing($this->trade->getTraderId()))
        {
          $msg = 'owner';
          if (!$this->getUser()->isSameAs($this->trade->getTrader()))
          {
            $msg = $this->getContext()->getI18N()->__('You have to follow %trader% to get access to the trades',
                    array('%trader%' => $this->trade->getTrader()->getUsername()), 'action');
            $this->getUser()->setFlash('error', $msg);

            $this->redirect('trader_home', $this->trade->getTrader());
          }
        }
      }
    }
    $this->getUser()->setFlash('notice', $msg);
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->trade = $this->getRoute()->getObject();
    $this->forward404Unless($this->getUser()->isSameAs($this->trade->getTrader()));
    $this->form = $this->configuration->getForm($this->trade);
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $default = array(
        'published_at' => time(),
        'trader_id' => $this->getUser()->getTrader()->getId());

    $form->bind(array_merge($request->getParameter($form->getName()), $default), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

      try
      {
        $trade = $form->save();
      }
      catch (Doctrine_Validator_Exception $e)
      {

        $errorStack = $form->getObject()->getErrorStack();

        $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ? 's' : null) . " with validation errors: ";
        foreach ($errorStack as $field => $errors)
        {
          $message .= "$field (" . implode(", ", $errors) . "), ";
        }
        $message = trim($message, ', ');

        $this->getUser()->setFlash('error', $message);
        return sfView::SUCCESS;
      }

      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $trade)));

      if ($request->hasParameter('_save_and_add'))
      {
        $this->getUser()->setFlash('notice', $notice . ' You can add another one below.');

        $this->redirect(array('sf_route' => 'trade_show', 'sf_subject' => $trade));
      }
      else
      {
        $this->getUser()->setFlash('notice', $notice);

        $this->redirect(array('sf_route' => 'trade_show', 'sf_subject' => $trade));
      }
    }
    else
    {
      $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
    }
  }

}
